<?php
require_once APPLICATION_PATH.'/modules/event/const.inc';
require_once APPLICATION_PATH.'/modules/event/lib/CommonFunctions.php';
require_once 'lib/BaseAction.php';
require_once 'lib/ThriftLogger.php';


class TTEvent_Base_Action extends Tamtay_Base_Action
{
	
	protected $page_cache_key;
	protected $page_cache_timeout;
	public $_cache_prefix = EVENT_CACHE_PREFIX;
	
	public function init(){
		parent::init();
		//$this->_helper->addPath(APPLICATION_PATH.'/modules/event/helpers', 'Helper');
		Zend_Controller_Action_HelperBroker::addPath(APPLICATION_PATH . '/modules/event/helpers', 'Helper');
		$this->view->addHelperPath(APPLICATION_PATH  . '/modules/event/views/helpers', 'TTEvent_View_Helper');
	}
	
	public function getControllerHelper(){
		return $this->_helper;
	}

	public function	loadModel($model, $module='event')
	{
		return parent::loadModel($model, $module);
	}
	
	public function getCache($key, $prefix = EVENT_CACHE_PREFIX)
	{                      
		$key = $prefix . $key;
		return parent::getCache($key);
	}
	
	public function setCache($key, $data, $prefix = EVENT_CACHE_PREFIX, $timeout = 1200, $setdb = true)
	{
		$key = $prefix . $key;
		parent::setCache($key, $data, $timeout, $setdb);
	}
	
	public function deleteCache($key, $prefix = EVENT_CACHE_PREFIX)
	{
		$key = $prefix . $key;
		parent::deleteCache($key);
	}

	public function isCacheExist($key){
		
		$key = EVENT_CACHE_PREFIX . $key;
		
		if(isset($_GET['nocache']) && $_GET['nocache']==1)return false;
		$cache_eng=self::getCacheEngine();
		return $cache_eng->test($key);
	}
	
	public function setPageCacheInfo($key, $timeOut){
		
		$this->page_cache_key = $key;
		$this->page_cache_timeout = $timeOut;
	}
	
	public function setPageCache($content){
		if($this->page_cache_key)
			$this->setCache($this->page_cache_key, $content, $this->page_cache_timeout);
	}
	
	public function isPageCacheExist(){
		if(!$this->page_cache_key)
			return false;
		else if(!$this->isCacheExist($this->page_cache_key)){
			return false;
		}
		return true;
	}
	
	public function getPageCache(){
		if($this->page_cache_key){
			return $this->getCache($this->page_cache_key);
		}
		return null;
	}
	
	
	
	public function requireLogin()
	{
		$u = $this->getUser();
		if ($u->uid==0){
			$this->_helper->getHelper('Redirector')->gotoUrlAndExit(IDS_LOGIN_URL.'?ref='.urlencode("http://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']));
		}
	}
	
	public function isMod()
	{
		return $this->hasPerm(EVENT_PERM_ADMIN);
	}

	public function isAdmin()
	{
		return $this->isMod();
	}
	
	public function watchdog($uid, $type, $desc, $info="")
	{
		$w_model=$this->loadModel("Watchdog");
		$data=array(
			'type'  => $type,
			'uid'	=> $uid,
			'timestamp' => time(),
			'description' => $desc,
			'data' => $info
		);
		$w_model->insert($data);
	}
	
	public function addModLog($uid, $action, $ref, $note)    
    {
        parent::addModLog('event', $uid, $action, $ref, $note);
    } 
    
    public function sendLog($action, $uid, $eid, $targetId, $targetUid){
    	
    	if(is_array($targetId) && count($targetId)>0){
    		for($i=0; $i<count($targetId); $i++){
    			$tid = $targetId[$id];
    			$tuid = $targetUid[$id];
    			
		    	$logArr = array();
				$logArr['action'] = $action;
				$logArr['action_uid'] = $uid;
				$logArr['object_id'] = $eid;
				$logArr['target_id'] = $tid;
				$logArr['target_uid'] = $tuid;
				ThriftLogger::thrift_log_get_content($logArr);
    		}
    	}
    	else{
    		$logArr = array();
			$logArr['action'] = $action;
			$logArr['action_uid'] = $uid;
			$logArr['object_id'] = $eid;
			$logArr['target_id'] = $targetId;
			$logArr['target_uid'] = $targetUid;
			ThriftLogger::thrift_log_get_content($logArr);
    	}
    }
    
    public function gotoLogin($ref=""){
    	if($ref != ""){
    		$ref = "?ref=".$ref;
    	}
    	$this->_helper->getHelper('Redirector')->gotoUrlAndExit(IDS_LOGIN_URL.$ref);
    }
    
//va
        public function isAdminEvent()
    {
        $user = $this->getUser();
        $admins = require(APPLICATION_PATH . '/config/admin.php'); 
        if (array_key_exists($user->uid, $admins)){
            return TRUE;    
        }
        return FALSE;
    }
//va
	
}

